package com.dd.admin.business.card.controller;

import cn.hutool.core.bean.BeanUtil;
import com.dd.admin.common.aop.operationLog.aop.OperLog;
import com.dd.admin.common.aop.operationLog.aop.OperType;
import com.dd.admin.common.security.SecurityUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.dd.admin.common.model.UpdateGroup;
import com.dd.admin.common.model.result.ResultBean;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotBlank;
import com.dd.admin.business.card.entity.Card;
import com.dd.admin.business.card.domain.CardVo;
import com.dd.admin.business.card.domain.CardDto;
import com.dd.admin.business.card.service.CardService;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 727869402@qq.com
 * @since 2024-06-17
 */
@Api(tags = "")
@RestController
public class CardController {

    @Autowired
    CardService cardService;

    @ApiOperation(value = "-分页列表")
    @ApiOperationSupport(order = 1)
    @GetMapping("/admin/card/page")
    @OperLog(operModule = "获取卡项列表",operType = OperType.QUERY,operDesc = "获取卡项列表")
    public ResultBean<IPage<CardVo>> page(CardDto cardDto) {
        cardDto.setShopId(SecurityUtil.getLoginUser().getDeptId());
        IPage<CardVo> pageInfo = cardService.selectCardPage(cardDto);
        return ResultBean.success(pageInfo);
    }

    @ApiOperation(value = "-列表")
    @ApiOperationSupport(order = 2)
    @GetMapping("/admin/card/list")
    public ResultBean<List<CardVo>> list(CardDto cardDto) {
        cardDto.setShopId(SecurityUtil.getLoginUser().getDeptId());
        List<CardVo> list = cardService.selectCardList(cardDto);
        return ResultBean.success(list);
    }

    @ApiOperation(value = "-添加")
    @ApiOperationSupport(order = 3)
    @PostMapping("/admin/card/add")
    @OperLog(operModule = "添加卡项",operType = OperType.ADD,operDesc = "添加卡项")
    public ResultBean<Card> add(@RequestBody @Validated CardDto cardDto) {
        Card card = BeanUtil.copyProperties(cardDto, Card.class);
        cardService.save(card);
        return ResultBean.success(card);
    }

    @ApiOperation(value = "-查询")
    @ApiOperationSupport(order = 4)
    @GetMapping("/admin/card/{cardId}")
    public ResultBean<CardVo> get(@PathVariable @NotBlank String cardId) {
        Card card = cardService.getById(cardId);
        CardVo cardVo = BeanUtil.copyProperties(card,CardVo.class);
        return ResultBean.success(cardVo);
    }

    @ApiOperation(value = "-修改")
    @ApiOperationSupport(order = 5)
    @PostMapping("/admin/card/update")
    public ResultBean<Card> update(@RequestBody @Validated(UpdateGroup.class) CardDto cardDto) {
        Card card = BeanUtil.copyProperties(cardDto, Card.class);
        cardService.updateById(card);
        return ResultBean.success(card);
    }

    @ApiOperation(value = "-删除")
    @ApiOperationSupport(order = 6)
    @GetMapping("/admin/card/delete/{cardId}")
    public ResultBean<Card> delete(@PathVariable @NotBlank String cardId) {
        Boolean b = cardService.removeById(cardId);
        return ResultBean.success(b);
    }
}
